package com.sugar.subscribe.course.module.business.service.coachplan;

import cn.hutool.core.collection.CollUtil;
import com.sugar.subscribe.course.module.business.controller.app.coachplan.vo.AppCoachPlanRespVO;
import com.sugar.subscribe.course.module.business.controller.app.coachplan.vo.AppCoachPlanSaveReqVO;
import com.sugar.subscribe.course.module.business.dal.dataobject.serviceclass.ServiceClassDO;
import com.sugar.subscribe.course.module.business.dal.mysql.serviceclass.ServiceClassMapper;
import com.sugar.subscribe.course.module.business.util.AmountUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

import com.sugar.subscribe.course.module.business.controller.admin.coachplan.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachplan.CoachPlanDO;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;

import com.sugar.subscribe.course.module.business.dal.mysql.coachplan.CoachPlanMapper;

import static com.sugar.subscribe.course.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.subscribe.course.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.subscribe.course.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.subscribe.course.module.business.enums.ErrorCodeConstants.*;

/**
 * 教练课程方案 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class CoachPlanServiceImpl implements CoachPlanService {

    @Resource
    private CoachPlanMapper coachPlanMapper;
    @Resource
    private ServiceClassMapper serviceClassMapper;

    @Override
    public Long createCoachPlan(CoachPlanSaveReqVO createReqVO) {
        // 插入
        CoachPlanDO coachPlan = BeanUtils.toBean(createReqVO, CoachPlanDO.class);
        Long[] serviceIds = createReqVO.getServiceIds();
        coachPlan.setServicePId(serviceIds[0]).setServiceId(serviceIds[1]);
        coachPlanMapper.insert(coachPlan);

        // 返回
        return coachPlan.getId();
    }

    @Override
    public void updateCoachPlan(CoachPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateCoachPlanExists(updateReqVO.getId());
        // 更新
        CoachPlanDO updateObj = BeanUtils.toBean(updateReqVO, CoachPlanDO.class);
        Long[] serviceIds = updateReqVO.getServiceIds();
        updateObj.setServicePId(serviceIds[0]).setServiceId(serviceIds[1]);

        if (updateReqVO.getAuditStatus().equals(10))
            updateObj.setRefuseCause("");

        coachPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteCoachPlan(Long id) {
        // 校验存在
        validateCoachPlanExists(id);
        // 删除
        coachPlanMapper.deleteById(id);
    }

    @Override
        public void deleteCoachPlanListByIds(List<Long> ids) {
        // 删除
        coachPlanMapper.deleteByIds(ids);
        }


    private CoachPlanDO validateCoachPlanExists(Long id) {
        CoachPlanDO coachPlanDO = coachPlanMapper.selectById(id);
        if (coachPlanDO == null) {
            throw exception(COACH_PLAN_NOT_EXISTS);
        }
        return coachPlanDO;
    }

    @Override
    public CoachPlanDO getCoachPlan(Long id) {
        return validateCoachPlanExists(id);
    }

    @Override
    public PageResult<CoachPlanRespVO> getCoachPlanPage(CoachPlanPageReqVO pageReqVO) {
        return coachPlanMapper.selectPage(pageReqVO);
    }

    @Override
    public Long appCreateCoachPlan(AppCoachPlanSaveReqVO createReqVO, Long coachId) {
        CoachPlanDO coachPlan = BeanUtils.toBean(createReqVO, CoachPlanDO.class);
        Long[] serviceIds = createReqVO.getServiceIds();
        coachPlan.setServicePId(serviceIds[0]).setServiceId(serviceIds[1])
                .setPrice(AmountUtil.yuanToCent(createReqVO.getAmount()))
                .setCoachId(coachId);
        coachPlanMapper.insert(coachPlan);

        return coachPlan.getId();
    }

    @Override
    public void appUpdateCoachPlan(AppCoachPlanSaveReqVO updateReqVO, Long coachId) {
        // 校验存在
        CoachPlanDO coachPlanDO = validateCoachPlanExists(updateReqVO.getId());

        // 判断权限
        if (!coachPlanDO.getCoachId().equals(coachId))
            throw exception(NO_PERMISSION);

        CoachPlanDO coachPlan = BeanUtils.toBean(updateReqVO, CoachPlanDO.class);
        Long[] serviceIds = updateReqVO.getServiceIds();
        coachPlan.setServicePId(serviceIds[0]).setServiceId(serviceIds[1])
                .setPrice(AmountUtil.yuanToCent(updateReqVO.getAmount()))
                .setAuditStatus(0);
        coachPlanMapper.updateById(coachPlan);
    }

    @Override
    public AppCoachPlanRespVO appGetCoachPlan(Long id, Long coachId) {
        // 校验存在
        CoachPlanDO coachPlanDO = validateCoachPlanExists(id);

        // 判断权限
        if (!coachPlanDO.getCoachId().equals(coachId))
            throw exception(NO_PERMISSION);

        AppCoachPlanRespVO respVO = BeanUtils.toBean(coachPlanDO, AppCoachPlanRespVO.class);

        // 获取父子级名称
        ServiceClassDO pClass = serviceClassMapper.selectById(coachPlanDO.getServicePId());
        if (null != pClass)
            respVO.setServicePName(pClass.getName());
        ServiceClassDO cClass = serviceClassMapper.selectById(coachPlanDO.getServiceId());
        if (null != cClass)
            respVO.setServiceName(cClass.getName());

        respVO.setAmount(AmountUtil.centToYuan(coachPlanDO.getPrice()))
                .setPrice(null);

        return respVO;
    }

    @Override
    public PageResult<AppCoachPlanRespVO> appGetCoachPlanPage(PageParam pageParam, Long coachId) {
        PageResult<CoachPlanDO> pageResult = coachPlanMapper.appSelectPage(pageParam, coachId);

        Map<Long, String> allClassNames = serviceClassMapper.selectList()
                .stream()
                .collect(Collectors.toMap(ServiceClassDO::getId, ServiceClassDO::getName));

        return BeanUtils.toBean(pageResult, AppCoachPlanRespVO.class,
                o -> o.setServiceIds(new Long[]{o.getServicePId(), o.getServiceId()})
                        .setAmount(AmountUtil.centToYuan(o.getPrice()))
                        .setPrice(null)
                        .setServicePName(allClassNames.get(o.getServicePId()))
                        .setServiceName(allClassNames.get(o.getServiceId())));
    }

}